package com.bluegalaxy.sitescore.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.bluegalaxy.sitescore.common.enums.RecordStatus;
import com.bluegalaxy.sitescore.entity.TbSiteInfo;
import com.bluegalaxy.sitescore.service.ITbMessageService;
import com.bluegalaxy.sitescore.service.ITbSiteInfoService;
import com.foxinmy.weixin4j.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;

/**
 * Created by Administrator on 2017/11/25 0025.
 */
@Controller
@RequestMapping("/site")
public class SiteController extends BaseController {

    @Autowired
    private ITbSiteInfoService siteInfoService;

    @Autowired
    private ITbMessageService messageService;

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/list")
    public String allList(Model model) {
        return "/site/allList";
    }

    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/list/{province}/{city}")
    public String list(@PathVariable Integer province,@PathVariable Integer city,Model model) {
        model.addAttribute("provinceId",province);
        model.addAttribute("cityId",city);
        model.addAttribute("province",iHatProvinceService.getProvinceService(province));
        model.addAttribute("city",iHatCityService.getCity(province,city));
        return "/site/list";
    }
    @ResponseBody
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/getSiteList/{provinceId}/{cityId}")
    public String getSiteList(@PathVariable Integer provinceId,@PathVariable Integer cityId) {
        Page<TbSiteInfo> page = getPage();
        String province=iHatProvinceService.getProvinceService(provinceId);
        String city=iHatCityService.getCity(provinceId, cityId);
        return jsonPage(siteInfoService.getSiteInfoList(page,province,city));
    }

    @ResponseBody
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/querySiteList")
    public String getSiteListByIdAndName(Integer province,Integer  city,String sid,String name) {
        Page<TbSiteInfo> page = getPage();
      //  System.out.println("------"+sid+","+name+","+province+","+city);
        String provinceStr=iHatProvinceService.getProvinceService(province);
        String cityStr=iHatCityService.getCity(province, city);
        page = siteInfoService.getSiteInfoListByIdAndName(page,provinceStr,cityStr,sid,name);
        return jsonPage(page);
    }
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/add/{province}/{city}")
    public String add(@PathVariable Integer province,@PathVariable Integer city,Model model) {
        model.addAttribute("province", iHatProvinceService.getProvinceService(province));
        model.addAttribute("city", iHatCityService.getCity(province,city));
        model.addAttribute("provinceId", province);
        model.addAttribute("cityId", city);
        return "/site/add";
    }
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/edit/{siteId}")
    public String edit(@PathVariable Long siteId,Model model) {
        TbSiteInfo tbSiteInfo=siteInfoService.getSiteInfoById(siteId);
        model.addAttribute("province", tbSiteInfo.getProvince());
        model.addAttribute("city", tbSiteInfo.getCity());
        model.addAttribute("site", tbSiteInfo);
        return "/site/add";
    }
    @ResponseBody
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/save")
    public String list(TbSiteInfo tbSiteInfo) {
        boolean rlt = false;
        if (tbSiteInfo != null ) {
            if (tbSiteInfo.getId() != null ) {
                tbSiteInfo.setUpdate_time(new Date());
                rlt = siteInfoService.updateSelectiveById(tbSiteInfo);
            } else {
                tbSiteInfo.setProvince(iHatProvinceService.getProvinceService(tbSiteInfo.getProvinceId()));
                tbSiteInfo.setCity(iHatCityService.getCity(tbSiteInfo.getProvinceId(), tbSiteInfo.getCityId()));
                tbSiteInfo.setDist(StringUtil.EMPTY);
                tbSiteInfo.setCreate_by(getCurrentUserId().intValue());
                tbSiteInfo.setCreate_time(new Date());
                tbSiteInfo.setUpdate_time(new Date());
                tbSiteInfo.setScore(0f);
                tbSiteInfo.setStatus(RecordStatus.Effective.getValue().shortValue());
                rlt = siteInfoService.insertSelective(tbSiteInfo);
            }
        }
        return callbackSuccess(rlt);
    }
    @ResponseBody
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/delSite/{siteId}")
    public String delUser(@PathVariable Long siteId) {
        siteInfoService.deleteSiteInfoById(siteId);
        return Boolean.TRUE.toString();
    }

    @ResponseBody
    @com.baomidou.kisso.annotation.Permission("1001")
    @RequestMapping("/examine/{siteId}")
    public String examineSite(@PathVariable Long siteId) {
        siteInfoService.updateSiteInfoExamine(siteId);
        messageService.insertMessage(getCurrentUserId(),siteId.intValue());
        return Boolean.TRUE.toString();
    }
}
